import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.mapreduce.lib.input.*;

public class InvertedIndex {
	private static final Pattern alphaDigit = Pattern
			.compile("[\\p{L}\\p{N}]+");

	// In a real application, this would be an Apache Zookeeper shared counter
	static int corpusSize = 0;

	public static int getCorpusSize() {
		// TODO
		return 0;
	}

	static class Map extends Mapper<Text, Text, Text, PairTextDoubleWritable> {
		@Override
		protected void map(Text key, Text value, Context context) {
			// TODO
			
			Matcher m = alphaDigit.matcher(value.toString());
			while (m.find()) {
				String token = m.group();
				
				// TODO
			}

			// TODO
		}
	}

	static class Reduce extends
			Reducer<Text, PairTextDoubleWritable, Text, Text> {
		@Override
		protected void reduce(Text key,
				Iterable<PairTextDoubleWritable> values, Context context) {
			// TODO
		}
	}

	public static void retrieveStopWords(String filename) throws IOException {
		// TODO
	}

	public static boolean isStopWord(String token) {
		// TODO
		return false;
	}

	public static void buildInvertedIndex(String input, String output)
			throws IOException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(PairTextDoubleWritable.class);

		job.setMapperClass(Map.class);
		job.setReducerClass(Reduce.class);

		job.setInputFormatClass(KeyValueTextInputFormat.class);
		job.setOutputFormatClass(TextOutputFormat.class);

		FileInputFormat.setInputPaths(job, new Path(input));
		FileOutputFormat.setOutputPath(job, new Path(output));

		job.waitForCompletion(false);
	}
	
	public static class DocumentWeight implements Comparable<DocumentWeight>
	{
		String documentTitle;
		double weight;
		
		DocumentWeight(String t, double d)
		{
			documentTitle=t;
			weight=d;
		}
		
		@Override
		public int compareTo(DocumentWeight that) {
			// TODO
			return 0;
		}
	}
}
